package cc.rengu.igas.mcps.core.service.trans.stagepay;

import cc.rengu.igas.mcps.common.enums.RespCodeEnum;
import cc.rengu.igas.mcps.core.service.base.InstalQuerySevice;
import cc.rengu.igas.mcps.facade.bean.ProdStageInfo;
import cc.rengu.igas.mcps.facade.request.CreditInstalInfoQueryRequest;
import cc.rengu.igas.mcps.facade.response.CreditInstalInfoQueryResponse;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.model.MchntStageInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.util.ConvertUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangbin on 2020/3/31
 * 信用卡分期信息查询
 */
public class CreditInstalInfoQueryService extends InstalQuerySevice {

    @Override
    protected Object bizBeforeCust() throws Exception {
        CreditInstalInfoQueryRequest creditInstalInfoQueryRequest = new CreditInstalInfoQueryRequest();
        ConvertUtil.convertOutput(creditInstalInfoQueryRequest);
        return creditInstalInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        CreditInstalInfoQueryRequest creditInstalInfoQueryRequest = (CreditInstalInfoQueryRequest) request;
        BizResponse bizResponse = new BizResponse();
        String instId = creditInstalInfoQueryRequest.getHeader().getInstId();
        /*获取商户基本信息*/
        /*商户状态检查*/
        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(instId, creditInstalInfoQueryRequest.getMchntNo());
        if (null == mchntInfo) {
            rglog.error("商户<{}>信息不存在或者状态异常!", creditInstalInfoQueryRequest.getMchntNo());
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }

        /*连锁门店 非独立清算商户 需要判断一级商户的业务权限*/
        String mchntNoTem = creditInstalInfoQueryRequest.getMchntNo();
        if (null != mchntInfo.getIndependentDataFlag()) {
            if ("11".equals(mchntInfo.getMchntType()) && "N".equals(mchntInfo.getIndependentDataFlag())) {
                mchntNoTem = mchntInfo.getChainMchntNo();
            }
        }

        int pageIndex = creditInstalInfoQueryRequest.getPageIndex();
        int pageSize = creditInstalInfoQueryRequest.getPageSize();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageIndex(pageIndex);
        CreditInstalInfoQueryResponse creditInstalInfoQueryResponse = new CreditInstalInfoQueryResponse();
        creditInstalInfoQueryResponse.setPageIndex(pageIndex);
        creditInstalInfoQueryResponse.setPageSize(pageSize);

        List<MchntStageInfo> mchntStageInfoList = mchntService.getMchntStageInfo(instId, mchntNoTem);
        if (null == mchntStageInfoList || mchntStageInfoList.isEmpty()) {
            rglog.error("根据查询条件商户分期列表查询服务pageResult 为null !");
            creditInstalInfoQueryResponse.setTotalPage(0);
            creditInstalInfoQueryResponse.setTotalRows(0);
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            bizResponse.setResult(creditInstalInfoQueryResponse);
            return bizResponse;
        }
        creditInstalInfoQueryResponse.setTotalRows(mchntStageInfoList.size());

        List<ProdStageInfo> posStageList = new ArrayList<>();
        /* 根据分页查询条件截取相关数据记录 */
        for (MchntStageInfo mchntStageInfo : mchntStageInfoList) {
            ProdStageInfo prodStageInfo = new ProdStageInfo();
            prodStageInfo.setMchntNo(mchntNoTem);
            prodStageInfo.setMchntStageNum(String.valueOf(mchntStageInfo.getMchntStageNum()));
            prodStageInfo.setProdStageCode(mchntStageInfo.getProdStageCode());
            prodStageInfo.setProdStageName(mchntStageInfo.getProdStageName());
            prodStageInfo.setProdStageFeeType(mchntStageInfo.getProdStageFeeType());
            posStageList.add(prodStageInfo);
        }
        /* 查询结果赋值 */
        creditInstalInfoQueryResponse.setPosStageInfoList(posStageList);

        creditInstalInfoQueryResponse.setHeader(creditInstalInfoQueryRequest.getHeader());
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(creditInstalInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        CreditInstalInfoQueryResponse creditInstalInfoQueryResponse = (CreditInstalInfoQueryResponse) bizResponse.getResult();
        creditInstalInfoQueryResponse.setRespCode(bizResponse.getRespCode());
        creditInstalInfoQueryResponse.setRespDesc(bizResponse.getRespDesc());
        ConvertUtil.convertInput(creditInstalInfoQueryResponse);
    }
}
